<?php

/**
 * @version		$Id: hello.php 15 2009-11-02 18:37:15Z chdemko $
 * @package		Joomla16.Tutorials
 * @subpackage	Components
 * @copyright	Copyright (C) 2005 - 2010 Open Source Matters, Inc. All rights reserved.
 * @author		Christophe Demko
 * @link		http://joomlacode.org/gf/project/helloworld_1_6/
 * @license		License GNU General Public License version 2 or later
 */

// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla modelitem library
jimport('joomla.application.component.modelitem');

/**
 * HelloWorld Model
 */
class bgModelbg extends JModel
{
    var $_total = null;
    var $_pagination = null;
    
    public function __construct(){
    	parent::__construct();
 
        $mainframe = JFactory::getApplication();
 
        // Get pagination request variables
        $limit = $mainframe->getUserStateFromRequest('com_pgb.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
        $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
 
        // In case limit has been changed, adjust it
        $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
 
        $this->setState('limit', $limit);
        $this->setState('limitstart', $limitstart);
    }
    
    /**
	*Get data table DOWM add in pulldown
	*/
    function getDOWM(){
        $this->_db->setQuery("SELECT DOWM_CODE, DOWM_NAME FROM dowm");
        return $this->_db->loadObjectList();
    }
    
    /**
     *save data in database 
    **/
    function save($data = null,$addNew = false){
      $pool_cd = HelperBG::getIDAuto('pool','POOL_CODE');
      $gymt1_cd = HelperBG::getIDAuto('gymt','GYMT_CODE');
      $gymt2_cd = HelperBG::getIDAuto('gymt','GYMT_CODE') + 1;
      $dock_cd = HelperBG::getIDAuto('dock','DOCK_CODE');
      //check data for empty
      $null_pool = true;
      $null_gymt1 = true;
      $null_gymt2 = true;
      $null_dock = true;
      
      
      foreach ($data as $key => $value) {

        if(!empty($value) && $value != 9){
          if(substr($key, 0, 4) == 'POOL'){
            $null_pool = false;
          } 
          elseif(substr($key, 0, 4) == 'GYMT' && substr($key, -1, 1) == "1"){
            $null_gymt1 = false;
          } 
          elseif(substr($key, 0, 4) == 'GYMT' && substr($key, -1, 1) == "2"){
            $null_gymt2 = false;
          }
          elseif(substr($key, 0, 4) == 'DOCK'){
            $null_dock = false;
          } 
        } 

        if(strpos($key,'DATE') && $value == ""){
             $data[$key] = "0000-00-00 00:00:00";
        }

        if($value === null){
          $data[$key] = "NULL";
        }

      }

      /* Trieu added 2013/10/27 checkbox gymt 2: equipment */
      if(isset($_POST['cbeq1'])){
          $data['GYMT_MATF2'] = 1;
          $data['GYMT_MATF1'] = 0;
          $null_gymt2 = false;
      }else{
          $data['GYMT_MATF2'] = 0;
          $data['GYMT_MATF1'] = 0;
      }
      if(isset($_POST['cbeq2'])){
          $data['GYMT_MACF2'] = 1;
          $data['GYMT_MACF1'] = 0;
          $null_gymt2 = false;
      }else{
          $data['GYMT_MACF2'] = 0;
          $data['GYMT_MACF1'] = 0;
      }
      
      /* end */
      
      if($null_pool == true && $null_gymt1 == true && $null_gymt2 == true && $null_dock == true){
                  
        return 'empty';
      }

      $data['POOL_POLK'] = $data['POOL_POLK'] == "" ? "NULL" : $data['POOL_POLK'];
      $data['POOL_POHK'] = $data['POOL_POHK'] == "" ? "NULL" : $data['POOL_POHK'];
      $data['POOL_METK'] = $data['POOL_METK'] == "" ? "NULL" : $data['POOL_METK'];


      $data['GYMT_METK1'] = $data['GYMT_METK1'] == "" ? "NULL" : $data['GYMT_METK1'];
      $data['GYMT_MACF1'] = $data['GYMT_MACF1'] == "" ? "NULL" : $data['GYMT_MACF1'];
      $data['GYMT_MATF1'] = $data['GYMT_MATF1'] == "" ? "NULL" : $data['GYMT_MATF1'];

      $data['GYMT_METK2'] = $data['GYMT_METK2'] == "" ? "NULL" : $data['GYMT_METK2'];
      $data['GYMT_MACF2'] = $data['GYMT_MACF2'] == "" ? "NULL" : $data['GYMT_MACF2'];
      $data['GYMT_MATF2'] = $data['GYMT_MATF2'] == "" ? "NULL" : $data['GYMT_MATF2'];

      $data['DOCK_DOCM_CODE'] = $data['DOCK_DOCM_CODE'] == "" ? "NULL" : $data['DOCK_DOCM_CODE'];
      $data['DOCK_WATM_CODE'] = $data['DOCK_WATM_CODE'] == "" ? "NULL" : $data['DOCK_WATM_CODE'];
      $data['DOCK_TOYK'] = $data['DOCK_TOYK'] == "" ? "NULL" : $data['DOCK_TOYK'];
      $data['DOCK_SHWK'] = $data['DOCK_SHWK'] == "" ? "NULL" : $data['DOCK_SHWK'];
      $data['DOCK_SHHK'] = $data['DOCK_SHHK'] == "" ? "NULL" : $data['DOCK_SHHK'];
      $data['DOCK_CHAK'] = $data['DOCK_CHAK'] == "" ? "NULL" : $data['DOCK_CHAK'];
      $data['DOCK_PAKK'] = $data['DOCK_PAKK'] == "" ? "NULL" : $data['DOCK_PAKK'];
      $data['DOCK_FTMF'] = $data['DOCK_FTMF'] == "" ? "NULL" : $data['DOCK_FTMF'];
      $data['DOCK_DOWM_CODE'] = $data['DOCK_DOWM_CODE'] == "" ? "NULL" : $data['DOCK_DOWM_CODE'];
      $data['DOCK_RSVF'] = $data['DOCK_RSVF'] == "" ? "NULL" : $data['DOCK_RSVF'];

      

      $data['GYMT_DATE'] = $data['POOL_DATE'] = $data['DOCK_DATE'] = date('Y-m-d H:i:s');

      $aut = new bgAuthorize();
      $data['POOL_USER_CREATE'] = $data['GYMT_USER_CREATE'] = $data['DOCK_USER_CREATE'] = $aut->getUserID();  

      $data['POOL_WEBF'] = 1;
      $data['POOL_FLAG'] = 1; 

      $data['GYMT_WEBF'] = 1;
      $data['GYMT_FLAG'] = 1;

      $data['DOCK_WEBF'] = 1;
      $data['DOCK_FLAG'] = 1;

      /* checkbox gymt 2: equipment */
      if(isset($_POST['cbeq1'])){
          $data['GYMT_MATF2'] = 1;
          $data['GYMT_MATF1'] = 0;
      }else{
          $data['GYMT_MATF2'] = 0;
          $data['GYMT_MATF1'] = 0;
      }
      if(isset($_POST['cbeq2'])){
          $data['GYMT_MACF2'] = 1;
          $data['GYMT_MACF1'] = 0;
      }else{
          $data['GYMT_MACF2'] = 0;
          $data['GYMT_MACF1'] = 0;
      }
      // echo json_encode(array('data' => $data,
      //                   'null' => array('pool' => $null_pool, 'gymt1' => $null_gymt1, 'gymt2' => $null_gymt2, 'dock' => $null_dock )          
      // ));
      // die;

       if($addNew){
            
           $qr_pool = "INSERT INTO pool (POOL_CODE,POOL_OCBA_CODE,POOL_ADMN,POOL_TERM,POOL_POLK,POOL_POHK,POOL_METK,POOL_TBPO_CODE_LIST,POOL_DISP_CODE_LIST,POOL_CIAD,POOL_CICI,POOL_COAD,POOL_COCI,POOL_CNOT,POOL_NOTE,POOL_SDATE,POOL_TDATE,POOL_EDATE,POOL_DATE,POOL_FLAG,POOL_WEBF,POOL_USER_CREATE)
                             VALUES ($pool_cd,'$data[OCBA_CODE]','$data[POOL_ADMN]','$data[POOL_TERM]',$data[POOL_POLK],$data[POOL_POHK],$data[POOL_METK],'$data[POOL_TBPO_CODE_LIST]','$data[POOL_DISP_CODE_LIST]',$data[POOL_CIAD],$data[POOL_CICI],$data[POOL_COAD],$data[POOL_COCI],'$data[POOL_CNOT]','$data[POOL_NOTE]','$data[POOL_SDATE]','$data[POOL_TDATE]','$data[POOL_EDATE]','$data[POOL_DATE]',$data[POOL_FLAG],$data[POOL_WEBF],$data[POOL_USER_CREATE])"; 
       
           $qr_gymt1 = "INSERT INTO gymt (GYMT_CODE,GYMT_OCBA_CODE,GYMT_2NDK,GYMT_TBGY_CODE_LIST,GYMT_MATF,GYMT_MACF,GYMT_ADMN,GYMT_SDATE,GYMT_TDATE,GYMT_METK,GYMT_EDATE,GYMT_CIAD,GYMT_CICI,GYMT_COAD,GYMT_COCI,GYMT_CNOT,GYMT_NOTE,GYMT_DATE,GYMT_FLAG,GYMT_WEBF,GYMT_USER_CREATE)
                             VALUES ($gymt1_cd,'$data[OCBA_CODE]',1,'$data[GYMT_TBGY_CODE_LIST1]',$data[GYMT_MATF1],$data[GYMT_MACF1],'$data[GYMT_ADMN1]','$data[GYMT_SDATE1]','$data[GYMT_TDATE1]',$data[GYMT_METK1],'$data[GYMT_EDATE1]',$data[GYMT_CIAD1],$data[GYMT_CICI1],$data[GYMT_COAD1],$data[GYMT_COCI1],'$data[GYMT_CNOT1]','$data[GYMT_NOTE1]','$data[GYMT_DATE]',$data[GYMT_FLAG],$data[GYMT_WEBF],$data[GYMT_USER_CREATE])";
           
           $qr_gymt2 = "INSERT INTO gymt (GYMT_CODE,GYMT_OCBA_CODE,GYMT_2NDK,GYMT_TBGY_CODE_LIST,GYMT_MATF,GYMT_MACF,GYMT_ADMN,GYMT_SDATE,GYMT_TDATE,GYMT_EDATE,GYMT_CIAD,GYMT_CICI,GYMT_COAD,GYMT_COCI,GYMT_CNOT,GYMT_NOTE,GYMT_DATE,GYMT_FLAG,GYMT_WEBF,GYMT_USER_CREATE)
                             VALUES ($gymt2_cd,'$data[OCBA_CODE]',2,'$data[GYMT_TBGY_CODE_LIST2]',$data[GYMT_MATF2],$data[GYMT_MACF2],'$data[GYMT_ADMN2]','$data[GYMT_SDATE2]','$data[GYMT_TDATE2]','$data[GYMT_EDATE2]',$data[GYMT_CIAD2],$data[GYMT_CICI2],$data[GYMT_COAD2],$data[GYMT_COCI2],'$data[GYMT_CNOT2]','$data[GYMT_NOTE2]','$data[GYMT_DATE]',$data[GYMT_FLAG],$data[GYMT_WEBF],$data[GYMT_USER_CREATE])";

           $qr_dock = "INSERT INTO dock (DOCK_CODE,DOCK_OCBA_CODE,DOCK_TBDO_CODE_LIST,DOCK_ADMN,DOCK_SDATE,DOCK_TDATE,DOCK_POST,DOCK_EDATE,DOCK_ADD1,
                              DOCK_TEL,DOCK_FAX,DOCK_TERM,DOCK_DOCM_CODE,DOCK_FTMF,DOCK_DOWM_CODE,DOCK_TOYK,DOCK_SHWK,DOCK_SHHK,DOCK_CHAK,DOCK_PAKK,DOCK_WATM_CODE,
                              DOCK_WNAME,DOCK_WNAMEW,DOCK_WNOTE,DOCK_IMGD,DOCK_IMGW,DOCK_IMGS,DOCK_CIAD,DOCK_CICI,DOCK_COAD,DOCK_COCI,DOCK_CNOT,
                              DOCK_NOTE,DOCK_DATE,DOCK_FLAG,DOCK_WEBF,DOCK_USER_CREATE, DOCK_RSVF)
                             VALUES ($dock_cd,'$data[OCBA_CODE]','$data[DOCK_TBDO_CODE_LIST]','$data[DOCK_ADMN]','$data[DOCK_SDATE]',
                              '$data[DOCK_TDATE]','$data[DOCK_POST]','$data[DOCK_EDATE]','$data[DOCK_ADD1]','$data[DOCK_TEL]','$data[DOCK_FAX]',
                              '$data[DOCK_TERM]',$data[DOCK_DOCM_CODE],$data[DOCK_FTMF],$data[DOCK_DOWM_CODE],$data[DOCK_TOYK],$data[DOCK_SHWK],
                              $data[DOCK_SHHK],$data[DOCK_CHAK],$data[DOCK_PAKK],$data[DOCK_WATM_CODE],'$data[DOCK_WNAME]','$data[DOCK_WNAMEW]',
                              '$data[DOCK_WNOTE]','$data[DOCK_IMGD]','$data[DOCK_IMGW]','$data[DOCK_IMGS]',$data[DOCK_CIAD],$data[DOCK_CICI],
                              $data[DOCK_COAD],$data[DOCK_COCI],'$data[DOCK_CNOT]','$data[DOCK_NOTE]','$data[DOCK_DATE]',$data[DOCK_FLAG],
                              $data[DOCK_WEBF],$data[DOCK_USER_CREATE], $data[DOCK_RSVF])";
           
           $check = false;
           //insert to table pool
           
           
           if(!$null_pool){
               $this->_db->setQuery($qr_pool); 

               if($this->_db->query()){
                    $check = true;
               }            
           }

           //insert to table gymt
          
           if(!$null_gymt1){
               $this->_db->setQuery($qr_gymt1);
               if($this->_db->query()){
                    $check = true;
               }             
           }      
           if($data['GYMT_2NDK'] == 2){                
               if(!$null_gymt2){
                   $this->_db->setQuery($qr_gymt2);
                   if($this->_db->query()){
                       $check = true;
                   }              
               }             
           }   
           //insert to table dock
           
           if(!$null_dock){
               $this->_db->setQuery($qr_dock);  
               // echo $qr_dock;
               // die;     
               if($this->_db->query()){
                    $check = true;
               }             
           }
           if($check){
                return true;
           }else{
                return false;
           }
                     
       }else{
            
            //KIEM TRA POOL: THEM HAY CAP NHAT 
            $check = false;
            if(!$null_pool){
                if($this->checkExistPOOL_CODE($data['POOL_CODE'])){
                    $qr_pool = "UPDATE pool
                                SET  POOL_OCBA_CODE='$data[OCBA_CODE]',POOL_ADMN='$data[POOL_ADMN]',POOL_TERM='$data[POOL_TERM]',POOL_POLK=$data[POOL_POLK],POOL_POHK=$data[POOL_POHK]
                                                ,POOL_METK=$data[POOL_METK],POOL_TBPO_CODE_LIST='$data[POOL_TBPO_CODE_LIST]',POOL_DISP_CODE_LIST='$data[POOL_DISP_CODE_LIST]',POOL_CIAD=$data[POOL_CIAD]
                                                ,POOL_CICI=$data[POOL_CICI],POOL_COAD=$data[POOL_COAD],POOL_COCI=$data[POOL_COCI],POOL_CNOT='$data[POOL_CNOT]'
                                                ,POOL_NOTE='$data[POOL_NOTE]',POOL_SDATE='$data[POOL_SDATE]',POOL_TDATE='$data[POOL_TDATE]'
                                                ,POOL_EDATE='$data[POOL_EDATE]',POOL_DATE='$data[POOL_DATE]',POOL_FLAG=$data[POOL_FLAG],POOL_WEBF=$data[POOL_WEBF],POOL_USER_CREATE=$data[POOL_USER_CREATE]
                                WHERE POOL_CODE = $data[POOL_CODE]";
                }else{
                    $qr_pool = "INSERT INTO pool (POOL_CODE,POOL_OCBA_CODE,POOL_ADMN,POOL_TERM,POOL_POLK,POOL_POHK,POOL_METK,POOL_TBPO_CODE_LIST,POOL_DISP_CODE_LIST,POOL_CIAD,POOL_CICI,POOL_COAD,POOL_COCI,POOL_CNOT,POOL_NOTE,POOL_SDATE,POOL_TDATE,POOL_EDATE,POOL_DATE,POOL_FLAG,POOL_WEBF,POOL_USER_CREATE)
                                VALUES ($pool_cd,'$data[OCBA_CODE]','$data[POOL_ADMN]','$data[POOL_TERM]',$data[POOL_POLK],$data[POOL_POHK],$data[POOL_METK],'$data[POOL_TBPO_CODE_LIST]','$data[POOL_DISP_CODE_LIST]',$data[POOL_CIAD],$data[POOL_CICI],$data[POOL_COAD],$data[POOL_COCI],'$data[POOL_CNOT]','$data[POOL_NOTE]','$data[POOL_SDATE]','$data[POOL_TDATE]','$data[POOL_EDATE]','$data[POOL_DATE]',$data[POOL_FLAG],$data[POOL_WEBF],$data[POOL_USER_CREATE])";
                }
                //insert to table pool      
                $this->_db->setQuery($qr_pool);       
                if($this->_db->query()){
                    $check = true;
                }
            }
            
            //KIEM TRA GYMT 1: THEM HAY CAP NHAT 
            if(!$null_gymt1){
                if($this->checkExistGYMT_CODE($data['GYMT_CODE1'])){
                    $qr_gymt1 = "UPDATE gymt
                                 SET  GYMT_OCBA_CODE='$data[OCBA_CODE]',GYMT_TBGY_CODE_LIST='$data[GYMT_TBGY_CODE_LIST1]',GYMT_MATF=$data[GYMT_MATF1],GYMT_MACF=$data[GYMT_MACF1],GYMT_ADMN='$data[GYMT_ADMN1]'
                                            ,GYMT_SDATE='$data[GYMT_SDATE1]',GYMT_TDATE='$data[GYMT_TDATE1]',GYMT_METK=$data[GYMT_METK1],GYMT_EDATE='$data[GYMT_EDATE1]'
                                            ,GYMT_CIAD=$data[GYMT_CIAD1],GYMT_CICI=$data[GYMT_CICI1],GYMT_COAD=$data[GYMT_COAD1],GYMT_COCI=$data[GYMT_COCI1],GYMT_CNOT='$data[GYMT_CNOT1]'
                                            ,GYMT_NOTE='$data[GYMT_NOTE1]',GYMT_DATE='$data[GYMT_DATE]',GYMT_FLAG=$data[GYMT_FLAG],GYMT_WEBF=$data[GYMT_WEBF],GYMT_USER_CREATE=$data[GYMT_USER_CREATE]
                                 WHERE GYMT_CODE = $data[GYMT_CODE1]";
                }else{
                    $qr_gymt1 = "INSERT INTO gymt (GYMT_CODE,GYMT_OCBA_CODE,GYMT_2NDK,GYMT_TBGY_CODE_LIST,GYMT_MATF,GYMT_MACF,GYMT_ADMN,GYMT_SDATE,GYMT_TDATE,GYMT_METK,GYMT_EDATE,GYMT_CIAD,GYMT_CICI,GYMT_COAD,GYMT_COCI,GYMT_CNOT,GYMT_NOTE,GYMT_DATE,GYMT_FLAG,GYMT_WEBF,GYMT_USER_CREATE)
                                 VALUES ($gymt1_cd,'$data[OCBA_CODE]',1,'$data[GYMT_TBGY_CODE_LIST1]',$data[GYMT_MATF1],$data[GYMT_MACF1],'$data[GYMT_ADMN1]','$data[GYMT_SDATE1]','$data[GYMT_TDATE1]',$data[GYMT_METK1],'$data[GYMT_EDATE1]',$data[GYMT_CIAD1],$data[GYMT_CICI1],$data[GYMT_COAD1],$data[GYMT_COCI1],'$data[GYMT_CNOT1]','$data[GYMT_NOTE1]','$data[GYMT_DATE]',$data[GYMT_FLAG],$data[GYMT_WEBF],$data[GYMT_USER_CREATE])";
                }
                //insert to table gymt 
                $this->_db->setQuery($qr_gymt1);
                if($this->_db->query()){
                    $check = true;
                }
            }
            
            //XOA GYMT2
            if($data['GYMT_2NDK'] == 1){
                
                if($data['GYMT_CODE2'] != null){
                    $qr = "DELETE FROM gymt WHERE GYMT_CODE = $data[GYMT_CODE2]";
                    $this->_db->setQuery($qr);
                    $this->_db->query();
                }
            }
            
           //KIEM TRA GYMT 2: THEM HAY CAP NHAT 
           if($data['GYMT_2NDK'] == 2){
               if(!$null_gymt2){
                   if($this->checkExistGYMT_CODE($data['GYMT_CODE2'])){
                        $qr_gymt2 = "UPDATE gymt 
                                    SET GYMT_OCBA_CODE='$data[OCBA_CODE]',GYMT_TBGY_CODE_LIST='$data[GYMT_TBGY_CODE_LIST2]',GYMT_MATF=$data[GYMT_MATF2],GYMT_MACF=$data[GYMT_MACF2],GYMT_ADMN='$data[GYMT_ADMN2]'
                                                ,GYMT_SDATE='$data[GYMT_SDATE2]',GYMT_TDATE='$data[GYMT_TDATE2]',GYMT_EDATE='$data[GYMT_EDATE2]',GYMT_CIAD=$data[GYMT_CIAD2]
                                                ,GYMT_CICI=$data[GYMT_CICI2],GYMT_COAD=$data[GYMT_COAD2],GYMT_COCI=$data[GYMT_COCI2],GYMT_CNOT='$data[GYMT_CNOT2]'
                                                ,GYMT_NOTE='$data[GYMT_NOTE2]',GYMT_DATE='$data[GYMT_DATE]',GYMT_FLAG=$data[GYMT_FLAG],GYMT_WEBF=$data[GYMT_WEBF],GYMT_USER_CREATE=$data[GYMT_USER_CREATE]
                                    WHERE GYMT_CODE = $data[GYMT_CODE2]";
                   }else{
                        $qr_gymt2 = "INSERT INTO gymt (GYMT_CODE,GYMT_OCBA_CODE,GYMT_2NDK,GYMT_TBGY_CODE_LIST,GYMT_MATF,GYMT_MACF,GYMT_ADMN,GYMT_SDATE,GYMT_TDATE,GYMT_EDATE,GYMT_CIAD,GYMT_CICI,GYMT_COAD,GYMT_COCI,GYMT_CNOT,GYMT_NOTE,GYMT_DATE,GYMT_FLAG,GYMT_WEBF,GYMT_USER_CREATE)
                                            VALUES ($gymt2_cd,'$data[OCBA_CODE]',2,'$data[GYMT_TBGY_CODE_LIST2]',$data[GYMT_MATF2],$data[GYMT_MACF2],'$data[GYMT_ADMN2]','$data[GYMT_SDATE2]','$data[GYMT_TDATE2]','$data[GYMT_EDATE2]',$data[GYMT_CIAD2],$data[GYMT_CICI2],$data[GYMT_COAD2],$data[GYMT_COCI2],'$data[GYMT_CNOT2]','$data[GYMT_NOTE2]','$data[GYMT_DATE]',$data[GYMT_FLAG],$data[GYMT_WEBF],$data[GYMT_USER_CREATE])";
                   }
                   //insert to table gymt
                   $this->_db->setQuery($qr_gymt2);
                   if($this->_db->query()){
                       $check = true;
                   }
               }
           }
           //KIEM TRA DOCK: THEM HAY CAP NHAT
           if(!$null_dock){
               if($this->checkExistDOCK_CODE($data['DOCK_CODE'])){
                    $qr_dock = "UPDATE dock
                                SET  DOCK_OCBA_CODE='$data[OCBA_CODE]',DOCK_TBDO_CODE_LIST='$data[DOCK_TBDO_CODE_LIST]',DOCK_ADMN='$data[DOCK_ADMN]',DOCK_SDATE='$data[DOCK_SDATE]'
                                                    ,DOCK_TDATE='$data[DOCK_TDATE]',DOCK_POST='$data[DOCK_POST]',DOCK_EDATE='$data[DOCK_EDATE]',DOCK_ADD1='$data[DOCK_ADD1]',DOCK_TEL='$data[DOCK_TEL]'
                                                    ,DOCK_FAX='$data[DOCK_FAX]',DOCK_TERM='$data[DOCK_TERM]',DOCK_DOCM_CODE=$data[DOCK_DOCM_CODE],DOCK_FTMF=$data[DOCK_FTMF],DOCK_DOWM_CODE=$data[DOCK_DOWM_CODE]
                                                    ,DOCK_TOYK=$data[DOCK_TOYK],DOCK_SHWK=$data[DOCK_SHWK],DOCK_SHHK=$data[DOCK_SHHK],DOCK_CHAK=$data[DOCK_CHAK],DOCK_PAKK=$data[DOCK_PAKK],DOCK_WATM_CODE=$data[DOCK_WATM_CODE]
                                                    ,DOCK_WNAME='$data[DOCK_WNAME]',DOCK_WNAMEW='$data[DOCK_WNAMEW]',DOCK_WNOTE='$data[DOCK_WNOTE]',DOCK_IMGD='$data[DOCK_IMGD]',DOCK_IMGW='$data[DOCK_IMGW]',DOCK_IMGS='$data[DOCK_IMGS]'
                                                    ,DOCK_CIAD=$data[DOCK_CIAD],DOCK_CICI=$data[DOCK_CICI],DOCK_COAD=$data[DOCK_COAD],DOCK_COCI=$data[DOCK_COCI],DOCK_CNOT='$data[DOCK_CNOT]',DOCK_NOTE='$data[DOCK_NOTE]'
                                                    ,DOCK_DATE='$data[DOCK_DATE]',DOCK_FLAG=$data[DOCK_FLAG],DOCK_WEBF=$data[DOCK_WEBF],DOCK_USER_CREATE=$data[DOCK_USER_CREATE]   
                                                    ,DOCK_RSVF=$data[DOCK_RSVF] 
                                WHERE DOCK_CODE = $data[DOCK_CODE]";
               }else{
                    $qr_dock = "INSERT INTO dock (DOCK_CODE,DOCK_OCBA_CODE,DOCK_TBDO_CODE_LIST,DOCK_ADMN,DOCK_SDATE,DOCK_TDATE,DOCK_POST,DOCK_EDATE,DOCK_ADD1,
                              DOCK_TEL,DOCK_FAX,DOCK_TERM,DOCK_DOCM_CODE,DOCK_FTMF,DOCK_DOWM_CODE,DOCK_TOYK,DOCK_SHWK,DOCK_SHHK,DOCK_CHAK,DOCK_PAKK,DOCK_WATM_CODE,
                              DOCK_WNAME,DOCK_WNAMEW,DOCK_WNOTE,DOCK_IMGD,DOCK_IMGW,DOCK_IMGS,DOCK_CIAD,DOCK_CICI,DOCK_COAD,DOCK_COCI,DOCK_CNOT,
                              DOCK_NOTE,DOCK_DATE,DOCK_FLAG,DOCK_WEBF,DOCK_USER_CREATE, DOCK_RSVF)
                             VALUES ($dock_cd,'$data[OCBA_CODE]','$data[DOCK_TBDO_CODE_LIST]','$data[DOCK_ADMN]','$data[DOCK_SDATE]',
                              '$data[DOCK_TDATE]','$data[DOCK_POST]','$data[DOCK_EDATE]','$data[DOCK_ADD1]','$data[DOCK_TEL]','$data[DOCK_FAX]',
                              '$data[DOCK_TERM]',$data[DOCK_DOCM_CODE],$data[DOCK_FTMF],$data[DOCK_DOWM_CODE],$data[DOCK_TOYK],$data[DOCK_SHWK],
                              $data[DOCK_SHHK],$data[DOCK_CHAK],$data[DOCK_PAKK],$data[DOCK_WATM_CODE],'$data[DOCK_WNAME]','$data[DOCK_WNAMEW]',
                              '$data[DOCK_WNOTE]','$data[DOCK_IMGD]','$data[DOCK_IMGW]','$data[DOCK_IMGS]',$data[DOCK_CIAD],$data[DOCK_CICI],
                              $data[DOCK_COAD],$data[DOCK_COCI],'$data[DOCK_CNOT]','$data[DOCK_NOTE]','$data[DOCK_DATE]',$data[DOCK_FLAG],
                              $data[DOCK_WEBF],$data[DOCK_USER_CREATE], $data[DOCK_RSVF])";
               }
               //insert to table dock      
               $this->_db->setQuery($qr_dock);       
               if($this->_db->query()){
                    $check = true;
                }
           }
           
           if($check){
                return true;
           }else{
                return false;
           }
       }
         
    }
    
    /**
     *save temp data in database 
    **/
    function savetemp($data = null,$addNew = false){

      $pool_cd = HelperBG::getIDAuto('pool','POOL_CODE');
      $gymt1_cd = HelperBG::getIDAuto('gymt','GYMT_CODE');
      $gymt2_cd = HelperBG::getIDAuto('gymt','GYMT_CODE') + 1;
      $dock_cd = HelperBG::getIDAuto('dock','DOCK_CODE');
      //check data for empty
      $null_pool = true;
      $null_gymt1 = true;
      $null_gymt2 = true;
      $null_dock = true;
      foreach ($data as $key => $value) {

        if(!empty($value) && $value != 9){
          if(substr($key, 0, 4) == 'POOL'){
            $null_pool = false;
          } 
          elseif(substr($key, 0, 4) == 'GYMT' && substr($key, -1, 1) == "1"){
            $null_gymt1 = false;
          } 
          elseif(substr($key, 0, 4) == 'GYMT' && substr($key, -1, 1) == "2"){
            $null_gymt2 = false;
          }
          elseif(substr($key, 0, 4) == 'DOCK'){
            $null_dock = false;
          } 
        } 

        if(strpos($key,'DATE') && $value == ""){
             $data[$key] = "0000-00-00 00:00:00";
        }

        if($value === null){
          $data[$key] = "NULL";
        }

      }

     if($null_pool == true && $null_gymt1 == true && $null_gymt2 == true && $null_dock == true){
                 
       return 'empty';
     }
 
      $data['POOL_POLK'] = $data['POOL_POLK'] == "" ? "NULL" : $data['POOL_POLK'];
      $data['POOL_POHK'] = $data['POOL_POHK'] == "" ? "NULL" : $data['POOL_POHK'];
      $data['POOL_METK'] = $data['POOL_METK'] == "" ? "NULL" : $data['POOL_METK'];


      $data['GYMT_METK1'] = $data['GYMT_METK1'] == "" ? "NULL" : $data['GYMT_METK1'];
      $data['GYMT_MACF1'] = $data['GYMT_MACF1'] == "" ? "NULL" : $data['GYMT_MACF1'];
      $data['GYMT_MATF1'] = $data['GYMT_MATF1'] == "" ? "NULL" : $data['GYMT_MATF1'];

      $data['GYMT_METK2'] = $data['GYMT_METK2'] == "" ? "NULL" : $data['GYMT_METK2'];
      $data['GYMT_MACF2'] = $data['GYMT_MACF2'] == "" ? "NULL" : $data['GYMT_MACF2'];
      $data['GYMT_MATF2'] = $data['GYMT_MATF2'] == "" ? "NULL" : $data['GYMT_MATF2'];

      $data['DOCK_DOCM_CODE'] = $data['DOCK_DOCM_CODE'] == "" ? "NULL" : $data['DOCK_DOCM_CODE'];
      $data['DOCK_WATM_CODE'] = $data['DOCK_WATM_CODE'] == "" ? "NULL" : $data['DOCK_WATM_CODE'];
      $data['DOCK_TOYK'] = $data['DOCK_TOYK'] == "" ? "NULL" : $data['DOCK_TOYK'];
      $data['DOCK_SHWK'] = $data['DOCK_SHWK'] == "" ? "NULL" : $data['DOCK_SHWK'];
      $data['DOCK_SHHK'] = $data['DOCK_SHHK'] == "" ? "NULL" : $data['DOCK_SHHK'];
      $data['DOCK_CHAK'] = $data['DOCK_CHAK'] == "" ? "NULL" : $data['DOCK_CHAK'];
      $data['DOCK_PAKK'] = $data['DOCK_PAKK'] == "" ? "NULL" : $data['DOCK_PAKK'];
      $data['DOCK_FTMF'] = $data['DOCK_FTMF'] == "" ? "NULL" : $data['DOCK_FTMF'];
      $data['DOCK_DOWM_CODE'] = $data['DOCK_DOWM_CODE'] == "" ? "NULL" : $data['DOCK_DOWM_CODE'];
      $data['DOCK_RSVF'] = $data['DOCK_RSVF'] == "" ? "NULL" : $data['DOCK_RSVF'];
      

      

      $data['GYMT_DATE'] = $data['POOL_DATE'] = $data['DOCK_DATE'] = date('Y-m-d H:i:s');

      $aut = new bgAuthorize();
      $data['POOL_USER_CREATE'] = $data['GYMT_USER_CREATE'] = $data['DOCK_USER_CREATE'] = $aut->getUserID();  

      $data['POOL_WEBF'] = 0;
      $data['POOL_FLAG'] = 0; 

      $data['GYMT_WEBF'] = 0;
      $data['GYMT_FLAG'] = 0;

      $data['DOCK_WEBF'] = 0;
      $data['DOCK_FLAG'] = 0;

      /* checkbox gymt 2: equipment */
      if(isset($_POST['cbeq1'])){
          $data['GYMT_MATF2'] = 1;
          $data['GYMT_MATF1'] = 0;
      }else{
          $data['GYMT_MATF2'] = 0;
          $data['GYMT_MATF1'] = 0;
      }
      if(isset($_POST['cbeq2'])){
          $data['GYMT_MACF2'] = 1;
          $data['GYMT_MACF1'] = 0;
      }else{
          $data['GYMT_MACF2'] = 0;
          $data['GYMT_MACF1'] = 0;
      }

       if($addNew){
           $qr_pool = "INSERT INTO pool (POOL_CODE,POOL_OCBA_CODE,POOL_ADMN,POOL_TERM,POOL_POLK,POOL_POHK,POOL_METK,POOL_TBPO_CODE_LIST,POOL_DISP_CODE_LIST,POOL_CIAD,POOL_CICI,POOL_COAD,POOL_COCI,POOL_CNOT,POOL_NOTE,POOL_SDATE,POOL_TDATE,POOL_EDATE,POOL_DATE,POOL_FLAG,POOL_WEBF,POOL_USER_CREATE)
                             VALUES ($pool_cd,'$data[OCBA_CODE]','$data[POOL_ADMN]','$data[POOL_TERM]',$data[POOL_POLK],$data[POOL_POHK],$data[POOL_METK],'$data[POOL_TBPO_CODE_LIST]','$data[POOL_DISP_CODE_LIST]',$data[POOL_CIAD],$data[POOL_CICI],$data[POOL_COAD],$data[POOL_COCI],'$data[POOL_CNOT]','$data[POOL_NOTE]','$data[POOL_SDATE]','$data[POOL_TDATE]','$data[POOL_EDATE]','$data[POOL_DATE]',$data[POOL_FLAG],$data[POOL_WEBF],$data[POOL_USER_CREATE])"; 
       
           $qr_gymt1 = "INSERT INTO gymt (GYMT_CODE,GYMT_OCBA_CODE,GYMT_2NDK,GYMT_TBGY_CODE_LIST,GYMT_MATF,GYMT_MACF,GYMT_ADMN,GYMT_SDATE,GYMT_TDATE,GYMT_METK,GYMT_EDATE,GYMT_CIAD,GYMT_CICI,GYMT_COAD,GYMT_COCI,GYMT_CNOT,GYMT_NOTE,GYMT_DATE,GYMT_FLAG,GYMT_WEBF,GYMT_USER_CREATE)
                             VALUES ($gymt1_cd,'$data[OCBA_CODE]',1,'$data[GYMT_TBGY_CODE_LIST1]',$data[GYMT_MATF1],$data[GYMT_MACF1],'$data[GYMT_ADMN1]','$data[GYMT_SDATE1]','$data[GYMT_TDATE1]',$data[GYMT_METK1],'$data[GYMT_EDATE1]',$data[GYMT_CIAD1],$data[GYMT_CICI1],$data[GYMT_COAD1],$data[GYMT_COCI1],'$data[GYMT_CNOT1]','$data[GYMT_NOTE1]','$data[GYMT_DATE]',$data[GYMT_FLAG],$data[GYMT_WEBF],$data[GYMT_USER_CREATE])";
           
           $qr_gymt2 = "INSERT INTO gymt (GYMT_CODE,GYMT_OCBA_CODE,GYMT_2NDK,GYMT_TBGY_CODE_LIST,GYMT_MATF,GYMT_MACF,GYMT_ADMN,GYMT_SDATE,GYMT_TDATE,GYMT_EDATE,GYMT_CIAD,GYMT_CICI,GYMT_COAD,GYMT_COCI,GYMT_CNOT,GYMT_NOTE,GYMT_DATE,GYMT_FLAG,GYMT_WEBF,GYMT_USER_CREATE)
                             VALUES ($gymt2_cd,'$data[OCBA_CODE]',2,'$data[GYMT_TBGY_CODE_LIST2]',$data[GYMT_MATF2],$data[GYMT_MACF2],'$data[GYMT_ADMN2]','$data[GYMT_SDATE2]','$data[GYMT_TDATE2]','$data[GYMT_EDATE2]',$data[GYMT_CIAD2],$data[GYMT_CICI2],$data[GYMT_COAD2],$data[GYMT_COCI2],'$data[GYMT_CNOT2]','$data[GYMT_NOTE2]','$data[GYMT_DATE]',$data[GYMT_FLAG],$data[GYMT_WEBF],$data[GYMT_USER_CREATE])";
           $qr_dock = "INSERT INTO dock (DOCK_CODE,DOCK_OCBA_CODE,DOCK_TBDO_CODE_LIST,DOCK_ADMN,DOCK_SDATE,DOCK_TDATE,DOCK_POST,DOCK_EDATE,DOCK_ADD1,
                              DOCK_TEL,DOCK_FAX,DOCK_TERM,DOCK_DOCM_CODE,DOCK_FTMF,DOCK_DOWM_CODE,DOCK_TOYK,DOCK_SHWK,DOCK_SHHK,DOCK_CHAK,DOCK_PAKK,DOCK_WATM_CODE,
                              DOCK_WNAME,DOCK_WNAMEW,DOCK_WNOTE,DOCK_IMGD,DOCK_IMGW,DOCK_IMGS,DOCK_CIAD,DOCK_CICI,DOCK_COAD,DOCK_COCI,DOCK_CNOT,
                              DOCK_NOTE,DOCK_DATE,DOCK_FLAG,DOCK_WEBF,DOCK_USER_CREATE, DOCK_RSVF)
                             VALUES ($dock_cd,'$data[OCBA_CODE]','$data[DOCK_TBDO_CODE_LIST]','$data[DOCK_ADMN]','$data[DOCK_SDATE]',
                              '$data[DOCK_TDATE]','$data[DOCK_POST]','$data[DOCK_EDATE]','$data[DOCK_ADD1]','$data[DOCK_TEL]','$data[DOCK_FAX]',
                              '$data[DOCK_TERM]',$data[DOCK_DOCM_CODE],$data[DOCK_FTMF],$data[DOCK_DOWM_CODE],$data[DOCK_TOYK],$data[DOCK_SHWK],
                              $data[DOCK_SHHK],$data[DOCK_CHAK],$data[DOCK_PAKK],$data[DOCK_WATM_CODE],'$data[DOCK_WNAME]','$data[DOCK_WNAMEW]',
                              '$data[DOCK_WNOTE]','$data[DOCK_IMGD]','$data[DOCK_IMGW]','$data[DOCK_IMGS]',$data[DOCK_CIAD],$data[DOCK_CICI],
                              $data[DOCK_COAD],$data[DOCK_COCI],'$data[DOCK_CNOT]','$data[DOCK_NOTE]','$data[DOCK_DATE]',$data[DOCK_FLAG],
                              $data[DOCK_WEBF],$data[DOCK_USER_CREATE], $data[DOCK_RSVF])";

           $check = false;
           //insert to table pool
           if(!$null_pool){
               $this->_db->setQuery($qr_pool);     

               if($this->_db->query()){
                    $check = true;
               }            
           }
           //insert to table gymt
           if(!$null_gymt1){
               $this->_db->setQuery($qr_gymt1);
               if($this->_db->query()){
                    $check = true;
               }             
           }      
           if($data['GYMT_2NDK'] == 2){
               if(!$null_gymt2){
                   $this->_db->setQuery($qr_gymt2);
                   if($this->_db->query()){
                       $check = true;
                   }              
               }             
           }   
           //insert to table dock
           if(!$null_dock){
               $this->_db->setQuery($qr_dock);  
  
               if($this->_db->query()){
                    $check = true;
               }             
           }
           if($check){
                return true;
           }else{
                return false;
           }
                     
       }else{
            
            //KIEM TRA POOL: THEM HAY CAP NHAT 
            
            $check = false;
            if(!$null_pool){
                if($this->checkExistPOOL_CODE($data['POOL_CODE'])){
                    $qr_pool = "UPDATE pool
                                SET  POOL_OCBA_CODE='$data[OCBA_CODE]',POOL_ADMN='$data[POOL_ADMN]',POOL_TERM='$data[POOL_TERM]',POOL_POLK=$data[POOL_POLK],POOL_POHK=$data[POOL_POHK]
                                                ,POOL_METK=$data[POOL_METK],POOL_TBPO_CODE_LIST='$data[POOL_TBPO_CODE_LIST]',POOL_DISP_CODE_LIST='$data[POOL_DISP_CODE_LIST]',POOL_CIAD=$data[POOL_CIAD]
                                                ,POOL_CICI=$data[POOL_CICI],POOL_COAD=$data[POOL_COAD],POOL_COCI=$data[POOL_COCI],POOL_CNOT='$data[POOL_CNOT]'
                                                ,POOL_NOTE='$data[POOL_NOTE]',POOL_SDATE='$data[POOL_SDATE]',POOL_TDATE='$data[POOL_TDATE]'
                                                ,POOL_EDATE='$data[POOL_EDATE]',POOL_DATE='$data[POOL_DATE]',POOL_FLAG=$data[POOL_FLAG],POOL_WEBF=$data[POOL_WEBF],POOL_USER_CREATE=$data[POOL_USER_CREATE]
                                WHERE POOL_CODE = $data[POOL_CODE]";
                }else{
                    $qr_pool = "INSERT INTO pool (POOL_CODE,POOL_OCBA_CODE,POOL_ADMN,POOL_TERM,POOL_POLK,POOL_POHK,POOL_METK,POOL_TBPO_CODE_LIST,POOL_DISP_CODE_LIST,POOL_CIAD,POOL_CICI,POOL_COAD,POOL_COCI,POOL_CNOT,POOL_NOTE,POOL_SDATE,POOL_TDATE,POOL_EDATE,POOL_DATE,POOL_FLAG,POOL_WEBF,POOL_USER_CREATE)
                                VALUES ($pool_cd,'$data[OCBA_CODE]','$data[POOL_ADMN]','$data[POOL_TERM]',$data[POOL_POLK],$data[POOL_POHK],$data[POOL_METK],'$data[POOL_TBPO_CODE_LIST]','$data[POOL_DISP_CODE_LIST]',$data[POOL_CIAD],$data[POOL_CICI],$data[POOL_COAD],$data[POOL_COCI],'$data[POOL_CNOT]','$data[POOL_NOTE]','$data[POOL_SDATE]','$data[POOL_TDATE]','$data[POOL_EDATE]','$data[POOL_DATE]',$data[POOL_FLAG],$data[POOL_WEBF],$data[POOL_USER_CREATE])";
                }
                //insert to table pool      
                $this->_db->setQuery($qr_pool);       
                if($this->_db->query()){
                    $check = true;
                }
            }
            
            //KIEM TRA GYMT 1: THEM HAY CAP NHAT 
            if(!$null_gymt1){
                if($this->checkExistGYMT_CODE($data['GYMT_CODE1'])){
                    $qr_gymt1 = "UPDATE gymt
                                 SET  GYMT_OCBA_CODE='$data[OCBA_CODE]',GYMT_TBGY_CODE_LIST='$data[GYMT_TBGY_CODE_LIST1]',GYMT_MATF=$data[GYMT_MATF1],GYMT_MACF=$data[GYMT_MACF1],GYMT_ADMN='$data[GYMT_ADMN1]'
                                            ,GYMT_SDATE='$data[GYMT_SDATE1]',GYMT_TDATE='$data[GYMT_TDATE1]',GYMT_METK=$data[GYMT_METK1],GYMT_EDATE='$data[GYMT_EDATE1]'
                                            ,GYMT_CIAD=$data[GYMT_CIAD1],GYMT_CICI=$data[GYMT_CICI1],GYMT_COAD=$data[GYMT_COAD1],GYMT_COCI=$data[GYMT_COCI1],GYMT_CNOT='$data[GYMT_CNOT1]'
                                            ,GYMT_NOTE='$data[GYMT_NOTE1]',GYMT_DATE='$data[GYMT_DATE]',GYMT_FLAG=$data[GYMT_FLAG],GYMT_WEBF=$data[GYMT_WEBF],GYMT_USER_CREATE=$data[GYMT_USER_CREATE]
                                 WHERE GYMT_CODE = $data[GYMT_CODE1]";
                }else{
                    $qr_gymt1 = "INSERT INTO gymt (GYMT_CODE,GYMT_OCBA_CODE,GYMT_2NDK,GYMT_TBGY_CODE_LIST,GYMT_MATF,GYMT_MACF,GYMT_ADMN,GYMT_SDATE,GYMT_TDATE,GYMT_METK,GYMT_EDATE,GYMT_CIAD,GYMT_CICI,GYMT_COAD,GYMT_COCI,GYMT_CNOT,GYMT_NOTE,GYMT_DATE,GYMT_FLAG,GYMT_WEBF,GYMT_USER_CREATE)
                                 VALUES ($gymt1_cd,'$data[OCBA_CODE]',1,'$data[GYMT_TBGY_CODE_LIST1]',$data[GYMT_MATF1],$data[GYMT_MACF1],'$data[GYMT_ADMN1]','$data[GYMT_SDATE1]','$data[GYMT_TDATE1]',$data[GYMT_METK1],'$data[GYMT_EDATE1]',$data[GYMT_CIAD1],$data[GYMT_CICI1],$data[GYMT_COAD1],$data[GYMT_COCI1],'$data[GYMT_CNOT1]','$data[GYMT_NOTE1]','$data[GYMT_DATE]',$data[GYMT_FLAG],$data[GYMT_WEBF],$data[GYMT_USER_CREATE])";
                }
                //insert to table gymt 
                $this->_db->setQuery($qr_gymt1);
                if($this->_db->query()){
                    $check = true;
                }
                
            }
            
            //XOA GYMT2
            if($data['GYMT_2NDK'] == 1){
                
                if($data['GYMT_CODE2'] != null){
                    $qr = "DELETE FROM gymt WHERE GYMT_CODE = $data[GYMT_CODE2]";
                    $this->_db->setQuery($qr);
                    $this->_db->query();
                }
            }
            
           //KIEM TRA GYMT 2: THEM HAY CAP NHAT 
           if($data['GYMT_2NDK'] == 2){
               if(!$null_gymt2){
                   if($this->checkExistGYMT_CODE($data['GYMT_CODE2'])){
                        $qr_gymt2 = "UPDATE gymt 
                                    SET GYMT_OCBA_CODE='$data[OCBA_CODE]',GYMT_TBGY_CODE_LIST='$data[GYMT_TBGY_CODE_LIST2]',GYMT_MATF=$data[GYMT_MATF2],GYMT_MACF=$data[GYMT_MACF2],GYMT_ADMN='$data[GYMT_ADMN2]'
                                                ,GYMT_SDATE='$data[GYMT_SDATE2]',GYMT_TDATE='$data[GYMT_TDATE2]',GYMT_EDATE='$data[GYMT_EDATE2]',GYMT_CIAD=$data[GYMT_CIAD2]
                                                ,GYMT_CICI=$data[GYMT_CICI2],GYMT_COAD=$data[GYMT_COAD2],GYMT_COCI=$data[GYMT_COCI2],GYMT_CNOT='$data[GYMT_CNOT2]'
                                                ,GYMT_NOTE='$data[GYMT_NOTE2]',GYMT_DATE='$data[GYMT_DATE]',GYMT_FLAG=$data[GYMT_FLAG],GYMT_WEBF=$data[GYMT_WEBF],GYMT_USER_CREATE=$data[GYMT_USER_CREATE]
                                    WHERE GYMT_CODE = $data[GYMT_CODE2]";
                   }else{
                        $qr_gymt2 = "INSERT INTO gymt (GYMT_CODE,GYMT_OCBA_CODE,GYMT_2NDK,GYMT_TBGY_CODE_LIST,GYMT_MATF,GYMT_MACF,GYMT_ADMN,GYMT_SDATE,GYMT_TDATE,GYMT_EDATE,GYMT_CIAD,GYMT_CICI,GYMT_COAD,GYMT_COCI,GYMT_CNOT,GYMT_NOTE,GYMT_DATE,GYMT_FLAG,GYMT_WEBF,GYMT_USER_CREATE)
                                            VALUES ($gymt2_cd,'$data[OCBA_CODE]',2,'$data[GYMT_TBGY_CODE_LIST2]',$data[GYMT_MATF2],$data[GYMT_MACF2],'$data[GYMT_ADMN2]','$data[GYMT_SDATE2]','$data[GYMT_TDATE2]','$data[GYMT_EDATE2]',$data[GYMT_CIAD2],$data[GYMT_CICI2],$data[GYMT_COAD2],$data[GYMT_COCI2],'$data[GYMT_CNOT2]','$data[GYMT_NOTE2]','$data[GYMT_DATE]',$data[GYMT_FLAG],$data[GYMT_WEBF],$data[GYMT_USER_CREATE])";
                   }
                   //insert to table gymt
                   $this->_db->setQuery($qr_gymt2);
                   if($this->_db->query()){
                       $check = true;
                   }
               }
           }
           //KIEM TRA DOCK: THEM HAY CAP NHAT
           if(!$null_dock){
               if($this->checkExistDOCK_CODE($data['DOCK_CODE'])){
                    $qr_dock = "UPDATE dock
                                SET  DOCK_OCBA_CODE='$data[OCBA_CODE]',DOCK_TBDO_CODE_LIST='$data[DOCK_TBDO_CODE_LIST]',DOCK_ADMN='$data[DOCK_ADMN]',DOCK_SDATE='$data[DOCK_SDATE]'
                                                    ,DOCK_TDATE='$data[DOCK_TDATE]',DOCK_POST='$data[DOCK_POST]',DOCK_EDATE='$data[DOCK_EDATE]',DOCK_ADD1='$data[DOCK_ADD1]',DOCK_TEL='$data[DOCK_TEL]'
                                                    ,DOCK_FAX='$data[DOCK_FAX]',DOCK_TERM='$data[DOCK_TERM]',DOCK_DOCM_CODE=$data[DOCK_DOCM_CODE],DOCK_FTMF=$data[DOCK_FTMF],DOCK_DOWM_CODE=$data[DOCK_DOWM_CODE]
                                                    ,DOCK_TOYK=$data[DOCK_TOYK],DOCK_SHWK=$data[DOCK_SHWK],DOCK_SHHK=$data[DOCK_SHHK],DOCK_CHAK=$data[DOCK_CHAK],DOCK_PAKK=$data[DOCK_PAKK],DOCK_WATM_CODE=$data[DOCK_WATM_CODE]
                                                    ,DOCK_WNAME='$data[DOCK_WNAME]',DOCK_WNAMEW='$data[DOCK_WNAMEW]',DOCK_WNOTE='$data[DOCK_WNOTE]',DOCK_IMGD='$data[DOCK_IMGD]',DOCK_IMGW='$data[DOCK_IMGW]',DOCK_IMGS='$data[DOCK_IMGS]'
                                                    ,DOCK_CIAD=$data[DOCK_CIAD],DOCK_CICI=$data[DOCK_CICI],DOCK_COAD=$data[DOCK_COAD],DOCK_COCI=$data[DOCK_COCI],DOCK_CNOT='$data[DOCK_CNOT]',DOCK_NOTE='$data[DOCK_NOTE]'
                                                    ,DOCK_DATE='$data[DOCK_DATE]',DOCK_FLAG=$data[DOCK_FLAG],DOCK_WEBF=$data[DOCK_WEBF],DOCK_USER_CREATE=$data[DOCK_USER_CREATE]
                                                    ,DOCK_RSVF=$data[DOCK_RSVF]     
                                WHERE DOCK_CODE = $data[DOCK_CODE]";
               }else{
                    $qr_dock = "INSERT INTO dock (DOCK_CODE,DOCK_OCBA_CODE,DOCK_TBDO_CODE_LIST,DOCK_ADMN,DOCK_SDATE,DOCK_TDATE,DOCK_POST,DOCK_EDATE,DOCK_ADD1,
                              DOCK_TEL,DOCK_FAX,DOCK_TERM,DOCK_DOCM_CODE,DOCK_FTMF,DOCK_DOWM_CODE,DOCK_TOYK,DOCK_SHWK,DOCK_SHHK,DOCK_CHAK,DOCK_PAKK,DOCK_WATM_CODE,
                              DOCK_WNAME,DOCK_WNAMEW,DOCK_WNOTE,DOCK_IMGD,DOCK_IMGW,DOCK_IMGS,DOCK_CIAD,DOCK_CICI,DOCK_COAD,DOCK_COCI,DOCK_CNOT,
                              DOCK_NOTE,DOCK_DATE,DOCK_FLAG,DOCK_WEBF,DOCK_USER_CREATE, DOCK_RSVF)
                             VALUES ($dock_cd,'$data[OCBA_CODE]','$data[DOCK_TBDO_CODE_LIST]','$data[DOCK_ADMN]','$data[DOCK_SDATE]',
                              '$data[DOCK_TDATE]','$data[DOCK_POST]','$data[DOCK_EDATE]','$data[DOCK_ADD1]','$data[DOCK_TEL]','$data[DOCK_FAX]',
                              '$data[DOCK_TERM]',$data[DOCK_DOCM_CODE],$data[DOCK_FTMF],$data[DOCK_DOWM_CODE],$data[DOCK_TOYK],$data[DOCK_SHWK],
                              $data[DOCK_SHHK],$data[DOCK_CHAK],$data[DOCK_PAKK],$data[DOCK_WATM_CODE],'$data[DOCK_WNAME]','$data[DOCK_WNAMEW]',
                              '$data[DOCK_WNOTE]','$data[DOCK_IMGD]','$data[DOCK_IMGW]','$data[DOCK_IMGS]',$data[DOCK_CIAD],$data[DOCK_CICI],
                              $data[DOCK_COAD],$data[DOCK_COCI],'$data[DOCK_CNOT]','$data[DOCK_NOTE]','$data[DOCK_DATE]',$data[DOCK_FLAG],
                              $data[DOCK_WEBF],$data[DOCK_USER_CREATE], $data[DOCK_RSVF])";
               }
               //insert to table dock      
               $this->_db->setQuery($qr_dock);       
               if($this->_db->query()){
                    $check = true;
                }
           }
           
           if($check){
                return true;
           }else{
                return false;
           }
       }
         
    }
    
    
    /**
     * Check OCBA_CODE exist
    **/
    public function checkExistOCBA_CODE($cid){
        $qr = "SELECT OCBA_CODE FROM ocba WHERE OCBA_FLAG = 1 AND OCBA_CODE = '$cid'";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
        if(!empty($result)){
            return true;
        }
        return false;
    }
    
    /**
     * Check OCBA_CODE exist when add new
    **/
    public function checkExistOCBA_PGB_NEW($ocba_code){
       $qr = "SELECT DISTINCT OCBA_CODE, OCBA_NAME, OCBA_FLAG, POOL_CODE, GYMT_2NDK, DOCK_CODE
                FROM (SELECT DISTINCT OCBA_CODE, OCBA_NAME, OCBA_FLAG, POOL_CODE, GYMT_2NDK, DOCK_CODE
                                    FROM ocba
                                    LEFT JOIN gymt ON OCBA_CODE = GYMT_OCBA_CODE
                                    LEFT JOIN pool ON OCBA_CODE = POOL_OCBA_CODE
                                    LEFT JOIN dock ON OCBA_CODE = DOCK_OCBA_CODE
                                    WHERE POOL_CODE IS NOT NULL 
                                    OR GYMT_CODE IS NOT NULL 
                                    OR DOCK_CODE IS NOT NULL AND OCBA_FLAG = 1) AS TB
                WHERE TB.OCBA_CODE = '$ocba_code'";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
        if(!empty($result)){
            return true;
        }
        return false;
    }
    /**
     * Check OCBA_CODE exist when edit
    **/
    public function checkExistOCBA_PGB_EDIT($ocba_code,$ocba_code_old){
       if($ocba_code != $ocba_code_old){
           $qr = "SELECT DISTINCT OCBA_CODE, OCBA_NAME, OCBA_FLAG, POOL_CODE, GYMT_2NDK, DOCK_CODE
                    FROM (SELECT DISTINCT OCBA_CODE, OCBA_NAME, OCBA_FLAG, POOL_CODE, GYMT_2NDK, DOCK_CODE
                                        FROM ocba
                                        LEFT JOIN gymt ON OCBA_CODE = GYMT_OCBA_CODE
                                        LEFT JOIN pool ON OCBA_CODE = POOL_OCBA_CODE
                                        LEFT JOIN dock ON OCBA_CODE = DOCK_OCBA_CODE
                                        WHERE POOL_CODE IS NOT NULL 
                                        OR GYMT_CODE IS NOT NULL 
                                        OR DOCK_CODE IS NOT NULL AND OCBA_FLAG = 1 OR OCBA_CODE = '$ocba_code_old') AS TB
                    WHERE TB.OCBA_CODE = '$ocba_code'";
        }else{
            return false;
        }
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
        if(!empty($result)){
            return true;
        }
        return false;
    }
    /**
     * Check GYMT_CODE exist
    **/
    public function checkExistGYMT_CODE($cid){
        $qr = "SELECT GYMT_CODE FROM gymt WHERE GYMT_CODE = $cid";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
        if(!empty($result)){
            return true;
        }
        return false;
    }
    /**
     * Check POOL_CODE exist
    **/
    public function checkExistPOOL_CODE($cid){
        $qr = "SELECT POOL_CODE FROM pool WHERE POOL_CODE = $cid";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
        if(!empty($result)){
            return true;
        }
        return false;
    }
    /**
     * Check DOCK_CODE exist
    **/
    public function checkExistDOCK_CODE($cid){
        $qr = "SELECT DOCK_CODE FROM dock WHERE DOCK_CODE = $cid";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
        if(!empty($result)){
            return true;
        }
        return false;
    }
    /**
     * GET list TBPO
    **/
    function getAllTBPO(){

	   $qr = "SELECT TBPO_CODE,TBPO_NAME,TBPO_FLAG FROM tbpo";
       // order by
       $qr .= " ORDER BY TBPO_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    /**
     * GET list extend facility
    **/
    function getAllExtendFac(){

	   $qr = "SELECT TBPO_CODE,TBPO_NAME FROM tbpo";
       // order by
       $qr .= " ORDER BY TBPO_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    /**
     * GET list disabled form DISP
    **/
    function getBarrierFreeList(){

	   $qr = "SELECT DISP_CODE,DISP_NAME FROM disp";
       // order by
       $qr .= " ORDER BY DISP_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();

       return $result;
	}
    
    /**
     * GET list disabled GYMT from TBGY
    **/
    function getGymtBarrierFreeList(){

	   $qr = "SELECT TBGY_CODE,TBGY_NAME FROM tbgy";
       // order by
       $qr .= " ORDER BY TBGY_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    /**
     * GET list disabled DOCK from TBDO
    **/
    function getDockBarrierFreeList(){

	   $qr = "SELECT TBDO_CODE,TBDO_NAME FROM tbdo";
       // order by
       $qr .= " ORDER BY TBDO_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    
    /**
     * GET list TBGY where flag = 1
    **/
    function getListEquipment(){

	   $qr = "SELECT TBGY_CODE,TBGY_NAME,TBGY_FLAG FROM tbgy WHERE TBGY_FLAG = 1";
       // order by
       $qr .= " ORDER BY TBGY_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    /**
     * GET list TBDO
    **/
    function getListTBDO(){

	   $qr = "SELECT TBDO_CODE,TBDO_NAME FROM tbdo";
       // order by
       $qr .= " ORDER BY TBDO_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    /**
     * GET POOL From POOL_CODE 
    **/
    function getPoolFromID($ocba_code){
        $qr = "SELECT * 
                FROM pool WHERE POOL_OCBA_CODE = '$ocba_code'";

        $this->_db->setQuery($qr);
        $result = $this->_db->loadObject();        
        return $result;
    }
    /**
     * GET POOL From POOL_CODE 
    **/
    function getDockFromID($ocba_code){
        $qr = "SELECT *
                FROM dock WHERE DOCK_OCBA_CODE = '$ocba_code'";

        $this->_db->setQuery($qr);
        $result = $this->_db->loadObject();        
        return $result;
    }
    /**
     * GET list GYMT FROM OCBA_CODE 
    **/
	function getGymtFromOCBA_ID($ocba_code){

	   $qr ="SELECT GYMT_CODE,GYMT_OCBA_CODE,GYMT_2NDK,GYMT_TBGY_CODE_LIST,GYMT_MATF,GYMT_MACF,GYMT_ADMN,GYMT_SDATE,GYMT_TDATE,GYMT_METK,
                        GYMT_EDATE,GYMT_CIAD,GYMT_CICI,GYMT_COAD,GYMT_COCI,GYMT_CNOT,GYMT_NOTE,GYMT_DATE,GYMT_FLAG,GYMT_WEBF
                    FROM gymt WHERE GYMT_OCBA_CODE = '$ocba_code'
                    ORDER BY GYMT_CODE ASC";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    /**
     * GET list OCBA_CODE
    **/
    function getListOCBA_CODE($ocba_code_old){
    
	   $qr = "SELECT OCBA_CODE FROM ocba WHERE OCBA_FLAG = 1 OR OCBA_CODE = '$ocba_code_old'";
       // order by
       $qr .= " ORDER BY OCBA_CODE ASC"; 
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
  /* Hung: check gymt 1 or 2  */
  function getGYMT($ocba_code){
    $qr = "SELECT GYMT_2NDK FROM gymt WHERE GYMT_OCBA_CODE = '$ocba_code'";
    $this->_db->setQuery($qr);
    $result = $this->_db->loadObjectList();
    return $result;
  }
    /**
     * GET list POOL_GYMT_DOCK 
    **/
	function getAll($list = null){

	   $qr ="SELECT DISTINCT OCBA_CODE, OCBA_NAME, POOL_POLK, DOCK_DOCM_CODE, MAX( GYMT_2NDK ) AS GYMT_2NDK,POOL_WEBF,GYMT_WEBF,DOCK_WEBF, DOCK_CODE, POOL_CODE,GYMT_CODE
                FROM ocba
                LEFT JOIN pool ON ocba.OCBA_CODE = pool.POOL_OCBA_CODE
                LEFT JOIN gymt ON ocba.OCBA_CODE = gymt.GYMT_OCBA_CODE
                LEFT JOIN dock ON ocba.OCBA_CODE = dock.DOCK_OCBA_CODE
                WHERE 
                (POOL_CODE IS NOT NULL 
                OR GYMT_2NDK IS NOT NULL 
                OR DOCK_CODE IS NOT NULL)
                ";
       
       $where = array();
       
        //check user group
           $aut=new bgAuthorize();
          // check  user login
           $group=$aut->getUserGroup();
           $u=JFactory::getUser();
           $u_ocba= $u->USEM_OCBA_CODE;
           if($group==11)
           {
                $where[] = "OCBA_CODE=$u_ocba";
            }
       // search
       if(isset($list['filter_search'])){
            $where[] = "(OCBA_NAME LIKE '%$list[filter_search]%' OR OCBA_CODE = '$list[filter_search]')";
       }
       
       if(isset($list['user_id'])){
            $where[] = "POOL_USER_CREATE = $list[user_id] AND GYMT_USER_CREATE = $list[user_id] AND DOCK_USER_CREATE = $list[user_id]";
       }
       
        if($where){
            $where = " AND ". implode(' AND ', $where);
        }else{
            $where = null;
        }
       
       // get total number record
       $this->_total = $this->getTotal($where);
       
       $qr .= $where;
       
       // order by
       $qr .= " GROUP BY OCBA_CODE, OCBA_NAME ORDER BY COALESCE(POOL_DATE,  GYMT_DATE, DOCK_DATE) DESC "; 
       
       // pagination
       $qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
     
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       
       
       return $result;
       
	}
    
    function getTotal($where = null){
        // Load the content if it doesn't already exist
        if ($this->_total === null) { 
            $query = "SELECT COUNT( v.OCBA_CODE ) 
                        FROM (
                        
                        SELECT DISTINCT OCBA_CODE, OCBA_NAME, POOL_POLK, DOCK_DOCM_CODE, MAX( GYMT_2NDK ) AS GYMT_2NDK
                        FROM  ocba 
                        LEFT JOIN  pool ON  ocba.OCBA_CODE =  pool.POOL_OCBA_CODE 
                        LEFT JOIN  gymt ON  ocba.OCBA_CODE =  gymt.GYMT_OCBA_CODE
                        LEFT JOIN  dock ON  ocba.OCBA_CODE =  dock.DOCK_OCBA_CODE
                        WHERE (
                        POOL_CODE IS NOT NULL 
                        OR GYMT_2NDK IS NOT NULL 
                        OR DOCK_CODE IS NOT NULL
                        ) $where GROUP BY OCBA_CODE, OCBA_NAME) AS v";
            
            $this->_db->setQuery($query);
            $this->_total = $this->_db->loadResult();
        }
        return $this->_total;
    } 
    
    function getPagination(){
        // Load the content if it doesn't already exist
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
        }
        return $this->_pagination;
    }
    
	function getAllUser(){
	   $qr = "SELECT * FROM #__users";
       
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       return $result;
	}
    
    function load(){
        
    }
    
    function getBoathouseType(){
        $qr = "SELECT * FROM docm"; 
        
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObjectList();
        return $result;
    }
    
     /* get list water classification */
    function getListWaterClass(){
        $qr = "SELECT * FROM watm WHERE WATM_FLAG = 1"; 
        
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObjectList();
        return $result; 
    }	
    /*  get boathouse water's edge code */
    function getListBoatEdgeCode(){
        $qr = "SELECT * FROM dowm WHERE DOWM_FLAG = 1"; 
        
        $this->_db->setQuery($qr);
        $result = $this->_db->loadObjectList();
        return $result; 
    }
    
    /*Get all center */
    function getAllCenter(){

	   $qr = "SELECT OCBA_CODE,OCBA_NAME FROM ocba WHERE OCBA_FLAG = 1";
       /* order by */
       $qr .= " ORDER BY OCBA_CODE DESC"; 
       
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       return $result;
	}
    /*Get all center empty */
    function getAllCenterEmpty($ocba_code){

	   $qr = "SELECT DISTINCT OCBA_CODE, OCBA_NAME, OCBA_FLAG, POOL_CODE, GYMT_2NDK, DOCK_CODE
                    FROM ocba
                    LEFT JOIN gymt ON OCBA_CODE = GYMT_OCBA_CODE
                    LEFT JOIN pool ON OCBA_CODE = POOL_OCBA_CODE
                    LEFT JOIN dock ON OCBA_CODE = DOCK_OCBA_CODE
                    WHERE OCBA_WEBF=1 
                    AND POOL_CODE IS NULL 
                    AND GYMT_CODE IS NULL 
                    AND DOCK_CODE IS NULL AND OCBA_FLAG = 1 OR OCBA_CODE = '$ocba_code'";
       /* order by */
       $qr .= " ORDER BY OCBA_CODE ASC"; 
       //echo $qr;
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       return $result;
	}
    /*Get all center empty */
    function getAllListCenterEmpty($list = null){

	   $qr = "SELECT DISTINCT OCBA_CODE, OCBA_NAME, OCBA_FLAG, POOL_CODE, GYMT_2NDK, DOCK_CODE
                    FROM (
                    
                    SELECT DISTINCT OCBA_CODE, OCBA_NAME, OCBA_FLAG, POOL_CODE, GYMT_2NDK, DOCK_CODE
                    FROM ocba
                    LEFT JOIN gymt ON OCBA_CODE = GYMT_OCBA_CODE
                    LEFT JOIN pool ON OCBA_CODE = POOL_OCBA_CODE
                    LEFT JOIN dock ON OCBA_CODE = DOCK_OCBA_CODE
                    WHERE POOL_CODE IS NULL 
                    AND GYMT_CODE IS NULL 
                    AND DOCK_CODE IS NULL 
                    AND OCBA_FLAG =1
                    OR OCBA_CODE = '$list[ocba_code_old]'
                    ) AS tb";
       // search
       if(!empty($list['OCBA_NAME'])){
            $where = " WHERE tb.OCBA_NAME LIKE '%$list[OCBA_NAME]%'";
       }else{
            $where = null;
       }
       $qr .= $where;
       /* order by */
       $qr .= " ORDER BY tb.OCBA_CODE ASC"; 
       
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       return $result;
	}
    function getAllListCenter($list = null){
    
	   $qr = "SELECT OCBA_CODE,OCBA_NAME FROM ocba WHERE OCBA_FLAG = 1";
       // search
       if(!empty($list['OCBA_NAME'])){
            $where = " AND OCBA_NAME LIKE '%$list[OCBA_NAME]%'";
       }else{
            $where = null;
       }
       
       // get total number record
       //$this->_total = $this->getTotalTown($where);
       
       $qr .= $where;
       
       // order by
       $qr .= " ORDER BY OCBA_CODE DESC"; 
       
       // pagination
       //$qr .= " LIMIT ".$this->getState('limitstart').",".$this->getState('limit');
          
       
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObjectList();
       
       return $result;
	}
    
    function centerName($ocba_code){
        $qr = "SELECT OCBA_NAME FROM ocba WHERE OCBA_CODE = '$ocba_code' AND OCBA_FLAG = 1";

        $this->_db->setQuery($qr);
        $result = $this->_db->loadObject();        
        return $result->OCBA_NAME;
    }
    
    public function remove($cid){
        $cid = implode(',', $cid);
        
    	$query_pool = "DELETE FROM pool WHERE POOL_OCBA_CODE IN($cid)";
        $query_gymt = "DELETE FROM gymt WHERE GYMT_OCBA_CODE IN($cid)";
        $query_dock = "DELETE FROM dock WHERE DOCK_OCBA_CODE IN($cid)";
        
        $this->_db->setQuery($query_pool);
        $this->_db->query();
        $this->_db->setQuery($query_gymt);
        $this->_db->query();
        $this->_db->setQuery($query_dock);
        $this->_db->query();
    }
    /**
     * GET LIST DOCK IMAGE FROM OCBA_CODE 
    **/
    public function getImgFromOCBA_CODE($cid){
        $qr = "SELECT DOCK_IMGD,DOCK_IMGW,DOCK_IMGS FROM dock WHERE DOCK_OCBA_CODE = '$cid'";
       // load data
       $this->_db->setQuery($qr);
       $result = $this->_db->loadObject();
       
       return $result;
    }
}